package com.example.sport.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sport.domain.AthleteEventResult;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AthleteEventResultMapper extends BaseMapper<AthleteEventResult> {

    @Select({
            "<script>",
            "SELECT ",
            "e.name AS eventName,",
            "u.name AS userName,",
            "c.leader AS leader,",
            "c.name AS collegeName,",
            "r.score AS score,",
            "r.id AS id,",
            "r.points AS points,",
            "ven.event_rank AS `rank`",
            "FROM t_result r",
            "JOIN t_event e ON r.event_id = e.id",
            "JOIN v_event_ranking ven ON r.id = ven.id",
            "JOIN t_user u ON r.user_id = u.id",
            "JOIN t_college c ON u.college_id = c.id",
            "<where>",
            "  <if test='userId != null and userId.trim() != \"\"'>",
            "    r.user_id = #{userId}",
            "  </if>",
            "</where>",
            " ORDER BY r.event_id,ven.event_rank",
            "</script>"
    })
    List<AthleteEventResult> getAthleteEventResultsByUserId(String userId);
    @Select({
            "<script>",
            "SELECT ",
            "e.name AS eventName,",
            "u.name AS userName,",
            "co.leader AS leader,",
            "co.name AS collegeName,",
            "r.score AS score,",
            "r.id AS id,",
            "r.points AS points,",
            "ven.event_rank AS `rank`",
            "FROM t_result r",
            "JOIN t_event e ON r.event_id = e.id",
            "JOIN v_event_ranking ven ON r.id = ven.id",
            "JOIN t_user u ON r.user_id = u.id",
            "JOIN t_college co ON u.college_id = co.id",
            "<where>",
            "  <if test='collegeId != null and collegeId.trim() != \"\"'>",
            "    co.id = #{collegeId}",
            "  </if>",
            "</where>",
            "ORDER BY r.event_id,ven.event_rank",
            "</script>"
    })
    List<AthleteEventResult> getAthleteEventResultsByCollegeId(String collegeId);
    @Select({
            "<script>",
            "SELECT ",
            "e.name AS eventName,",
            "u.name AS userName,",
            "co.leader AS leader,",
            "co.name AS collegeName,",
            "r.score AS score,",
            "r.id AS id,",
            "r.points AS points,",
            "ven.event_rank AS `rank`",
            "FROM t_result r",
            "JOIN t_event e ON r.event_id = e.id",
            "JOIN v_event_ranking ven ON r.id = ven.id",
            "JOIN t_user u ON r.user_id = u.id",
            "JOIN t_college co ON u.college_id = co.id",
            "<where>",
            "  <if test='id != null and id.trim() != \"\"'>",
            "    r.id = #{id}",
            "  </if>",
            "</where>",
            "ORDER BY r.event_id,ven.event_rank",
            "</script>"
    })
    AthleteEventResult getAthleteEventResultsById(String id);
}
